/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PatientInsert.java
 *
 * Created on Aug 18, 2011, 1:44:05 AM
 */
package patientManager;

import app.Global;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author Fun
 */
public class PatientInsert extends java.awt.Dialog {

    PatientTab parent;

    /** Creates new form PatientInsert */
    public PatientInsert(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.parent = (PatientTab) parent;
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        btnAddNew = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtFistName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtLastName = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtAddress = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtMobile = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtNote = new javax.swing.JTextField();

        setMinimumSize(new java.awt.Dimension(250, 400));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel2.setPreferredSize(new java.awt.Dimension(30, 100));

        btnAddNew.setText("Add new");
        btnAddNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddNewActionPerformed(evt);
            }
        });
        jPanel2.add(btnAddNew);

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        jPanel2.add(btnCancel);

        jPanel1.add(jPanel2, java.awt.BorderLayout.PAGE_END);

        jPanel3.setLayout(new java.awt.GridLayout(7, 0));

        jLabel2.setText("First Name");
        jPanel3.add(jLabel2);
        jPanel3.add(txtFistName);

        jLabel3.setText("Last Name");
        jPanel3.add(jLabel3);
        jPanel3.add(txtLastName);

        jLabel4.setText("Address");
        jPanel3.add(jLabel4);
        jPanel3.add(txtAddress);

        jLabel5.setText("Mobile");
        jPanel3.add(jLabel5);
        jPanel3.add(txtMobile);

        jLabel6.setText("Email");
        jPanel3.add(jLabel6);
        jPanel3.add(txtEmail);

        jLabel7.setText("Note");
        jPanel3.add(jLabel7);
        jPanel3.add(txtNote);

        jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);

        add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnAddNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddNewActionPerformed

        String regex = "^([\\w-\\.])+@([\\w])+\\.(\\w){2,6}(\\.([\\w]){2,4})*$";
        Pattern p = Pattern.compile(regex);
        Matcher matcher = p.matcher(txtEmail.getText());

        String phoneNumberPattern = "^[0-9]{7,11}$";
        boolean isMatch = Pattern.matches(phoneNumberPattern, txtMobile.getText());
        if (!matcher.find()) {
            JOptionPane.showMessageDialog(Global.getFrame(), "Email Not Ok");
            txtEmail.requestFocus();
            txtEmail.selectAll();
        } else if (!isMatch) {
            JOptionPane.showMessageDialog(Global.getFrame(), "Mobile Not Ok");
            txtMobile.requestFocus();
            txtMobile.selectAll();
        } else {
            Connection cn = Global.getConnection();
            PreparedStatement pst = null;
            try {
                pst = cn.prepareStatement("INSERT INTO patients(firstname,lastname,sid,address,mobile,email,note,created,modified)"
                        + " VALUES (?,?,?,?,?,?,?,?,?)");
                pst.setString(1, txtFistName.getText());
                pst.setString(2, txtLastName.getText());
                pst.setString(3, "1");
                pst.setString(4, txtAddress.getText());
                pst.setString(5, txtMobile.getText());
                pst.setString(6, txtEmail.getText());
                pst.setString(7, txtNote.getText());
                pst.setTimestamp(8, new Timestamp(System.currentTimeMillis()));
                pst.setTimestamp(9, new Timestamp(System.currentTimeMillis()));
                System.out.println(pst.toString());
                int i = pst.executeUpdate();
                if (i > 0) {
                    JOptionPane.showMessageDialog(Global.getFrame(), "SUCCESSFUL");
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(Global.getFrame(), "UNSUCCESSFUL");
                }
            } catch (SQLException ex) {
                Logger.getLogger(PatientInsert.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    pst.close();
                    cn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(PatientInsert.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }
    }//GEN-LAST:event_btnAddNewActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddNew;
    private javax.swing.JButton btnCancel;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtFistName;
    private javax.swing.JTextField txtLastName;
    private javax.swing.JTextField txtMobile;
    private javax.swing.JTextField txtNote;
    // End of variables declaration//GEN-END:variables
}
